﻿// Imports
using System;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web.Configuration;

namespace Nickel
{

    /// <summary>
    /// Summary description for Mail
    /// </summary>
    public class Mail
    {

        /// <summary>
        /// Sends e-mail
        /// </summary>
        /// <param name="to">To address</param>
        /// <param name="subject">Message subject</param>
        /// <param name="body">Message body</param>
        public static void Send(string to, string subject, string body)
        {

            // Call override with BCC default flag and blank sender
            Send(string.Empty, to, subject, body, false);

        }

        /// <summary>
        /// Sends e-mail
        /// </summary>
        /// <param name="to">To address</param>
        /// <param name="subject">Message subject</param>
        /// <param name="body">Message body</param>
        /// <param name="bcc">BCC sender</param>
        public static void Send(string to, string subject, string body, bool bcc)
        {

            // Call override with blank sender
            Send(string.Empty, to, subject, body, false);

        }

        /// <summary>
        /// Sends e-mail
        /// </summary>
        /// <param name="sender">From address</param>
        /// <param name="to">To address</param>
        /// <param name="subject">Message subject</param>
        /// <param name="body">Message body</param>
        /// <param name="bcc">BCC sender</param>
        public static void Send(string sender, string to, string subject, string body, bool bcc)
        {

            // Declare mail objects
            NetworkCredential credentials = null;
            MailAddress from = null;
            MailSettingsSectionGroup mailSettings = null;
            SmtpNetworkElement smtp = null;

            // Define default settings
            if (sender != string.Empty) 
                from = new MailAddress(sender);
            string host = "localhost";
            int port = 25;

            // Open web.config
            Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(@"~/web.config");
            if (webConfig != null)
            {

                // Get mail settings
                mailSettings = webConfig.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
                if (mailSettings != null)
                {

                    // Get SMTP settings
                    smtp = mailSettings.Smtp.Network;
                    if (smtp != null)
                    {

                        // Set up sender login
                        credentials = new NetworkCredential();
                        if (smtp.UserName != null && smtp.UserName.Length > 0) 
                            credentials.UserName = smtp.UserName;
                        if (smtp.Password != null && smtp.Password.Length > 0) 
                            credentials.Password = StringHelper.Decrypt(smtp.Password);
                        if (mailSettings.Smtp.From != null) 
                            from = new MailAddress(mailSettings.Smtp.From);

                        // Set server address
                        host = smtp.Host;
                        port = smtp.Port;

                    }

                }

            }

            // No sender specified
            if (from == null)
            {

                // Clean up
                credentials = null;
                from = null;
                mailSettings = null;
                smtp = null;
                webConfig = null;

                // Throw error
                throw new Exception("Sender address is required");

            }
            else
            {

                // Create the message
                MailMessage message = new MailMessage();
                message.From = from;
                message.To.Add(new MailAddress(to));
                if (bcc) message.Bcc.Add(from);
                message.ReplyToList.Add(from);
                message.Subject = subject;
                message.Body = body;

                // Set up the mail client
                SmtpClient client = new SmtpClient(host, port);

                // Check for SMTP config
                if (smtp != null)
                {

                    // Apply security settings
                    if (smtp.EnableSsl != null) 
                        client.EnableSsl = smtp.EnableSsl;
                    if (smtp.DefaultCredentials != null) 
                        client.UseDefaultCredentials = smtp.DefaultCredentials;

                }

                // Apply specified credentials
                if (credentials != null) 
                    client.Credentials = credentials;

                // Send the message
                client.Send(message);
                
                // Clean up
                client = null;

            }

            // Clean up
            credentials = null;
            from = null;
            mailSettings = null;
            smtp = null;
            webConfig = null;

        }

    }

}
